package com.hemaapp.yjnh.db;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.hemaapp.yjnh.bean.ProvinceCityDistrict;

import java.util.ArrayList;

/**
 * 所有的城市
 */
public class CityDBClient extends BaseDbHelper {
	private static CityDBClient mClient;
	private static Context mContext;

    String columns = "id,name,parentid,nodepath,namepath,charindex,level,orderby";

	private String tableName = ALL_CITYS;//默认是商品

	public CityDBClient(Context context) {
		super(context);
	}

	/**
	 * 插入一条记录
	 * 
	 * @return
	 */
	public boolean insert(ProvinceCityDistrict district) {
        String sql = "insert into "+tableName+" ("+ columns+ ") values ("
                + "?,?,?,?,?,"
                + "?,?,?)";
        Object[] bindArgs = new Object[] {
                district.getId(), district.getName(), district.getParentid(), district.getNodepath(),
                district.getNamepath(), district.getCharindex(), district.getLevel(), district.getOrderby()};

		SQLiteDatabase db = getWritableDatabase();
		boolean success = true;
        try {
            db.execSQL(sql, bindArgs);
        } catch (SQLException e) {
            success = false;
            Log.w("insert", "insert e=" + e);
        }
		db.close();
		return success;
	}

    public ArrayList<ProvinceCityDistrict> getCities(String parentId) {
        ArrayList<ProvinceCityDistrict> districts = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = null;
        try {
            cursor = db.query(tableName, null, "parentid=?", new String[]{parentId}, null, null, null);
//            cursor = db.rawQuery("select * from " + tableName+" where parentid='"+parentId+"'", null);
            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                for (int i = 0; i < cursor.getCount(); i++) {
                    ProvinceCityDistrict district = new ProvinceCityDistrict(cursor.getString(0), cursor.getString(1),
                            cursor.getString(2), cursor.getString(3),
                            cursor.getString(4), cursor.getString(5),
                            cursor.getString(6), cursor.getString(7));
                    districts.add(district);
                    cursor.moveToNext();
                }
            }

        }catch (Exception e) {
			e.printStackTrace();
        }
        if (cursor != null)
            cursor.close();
        db.close();
        return districts;
    }

	/**
	 * 清空
	 */
	public void clear() {
		SQLiteDatabase db = getWritableDatabase();
		try {
			db.execSQL("delete from " + tableName);
		} catch (Exception e) {
			Log.w("delete", "delete e=" + e);
		}
		db.close();
	}

	/**
	 * 判断表是否为空
	 * 
	 * @return
	 */
	public boolean isEmpty() {
		SQLiteDatabase db = getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + tableName, null);
		int num = cursor.getCount();
		cursor.close();
		db.close();
		return 0 == num;
	}

	public int getDataCount() {
		int num = 0;
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " + tableName, null);
		try {
			num = cursor.getCount();
		}catch (Exception e) {
			e.printStackTrace();
		}
        cursor.close();
        db.close();
        return num;
    }
}
